/************************************************************************************
 *	DirectX 10 Manager Class
 ************************************************************************************/

#ifndef DXMANAGER
#define DXMANAGER

#define SHADOW_MAP_SIZE	2048

#define NOMINMAX    // <-- this apparently isn't enough

#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>

#include <vector>

#include "vertexTypes.h"
#include "sceneGraph.h"
#include "ragdoll.h"

class dxManager
{
	/*******************************************************************
	* Members
	********************************************************************/	
private:

	//window handle shortcut
	HWND*						hWnd;
	
	//device vars
	ID3D10Device*				pD3DDevice;
	IDXGISwapChain*				pSwapChain;
	ID3D10RenderTargetView*		pRenderTargetView;
	ID3D10Texture2D*			pDepthStencil;
	ID3D10DepthStencilView*		pDepthStencilView;	
	D3D10_VIEWPORT				viewPort;

	//input layout and mesh
	ID3D10InputLayout*			pVertexLayout;
	//ID3DX10Mesh*				pMesh;

	//effects and techniques
	ID3D10Effect*				pBasicEffect;
	ID3D10EffectTechnique*		pBasicTechnique;
	ID3D10EffectTechnique*		pShadowDepthTechnique;
	ID3D10EffectTechnique*		pShadowRenderTechnique;

	//effect variable pointers
	ID3D10EffectMatrixVariable* pViewMatrixEffectVariable;
	ID3D10EffectMatrixVariable* pProjectionMatrixEffectVariable;
	ID3D10EffectMatrixVariable* pWorldMatrixEffectVariable;
	ID3D10EffectMatrixVariable* pWorldMatrixITVariable;
	ID3D10EffectMatrixVariable* pLightMatrixVariable;
	ID3D10EffectMatrixVariable* pTexMatrixVariable;

	ID3D10EffectVectorVariable* pWorldEyeVariable;
	ID3D10EffectVectorVariable* pWorldLightVariable;
	ID3D10EffectVectorVariable* pWorldLightPosVariable;
	
	//shadowmap res view
	ID3D10ShaderResourceView*	pTex;
	ID3D10EffectShaderResourceVariable* pTexVar;

	//normal map shader res view
	ID3D10ShaderResourceView*	pNorTex;
	ID3D10EffectShaderResourceVariable* pNorTexVar;
	
	//projection and view matrices
	D3DXMATRIX                  viewMatrix;
	D3DXMATRIX                  projectionMatrix;

	//technique
	D3D10_TECHNIQUE_DESC		techDesc;

	//shadowmapy?
	ID3D10Texture2D*			pShadowMap;
	ID3D10RenderTargetView*		pShadowMapView;
	ID3D10Texture2D*			pShadowDepth;
	ID3D10DepthStencilView*		pShadowDepthView;
	//ID3D10RenderTargetView*		pShadowDepthRT;

	//ID3D10Texture2D*			pNormalMap;


	//scene graph
	sceneGraph					gScene;

	//physics
	Physixer					phys;

	float						orbitX;
	float						orbitY;

	bool						gLH;
	bool						gLock;
	bool						gPhysics;
	bool						gHold;

	D3DXVECTOR3					gravityAcc;
	D3DXVECTOR3					motionAcc;
	
	
	/*******************************************************************
	* Methods
	********************************************************************/	
public:

	//constructor and destructor
	dxManager();
	~dxManager();

	//initialize directx device
	bool initialize(HWND*);

	bool loadCube();
	bool loadTeapot();
	bool loadRagdoll();

	bool createCornell();
	void removeCornell();
	void setCornellLock(bool d);

	void LH(bool s, HWND* hW);

	//scene function
	void renderScene();

	//read functions
	void readMap(unsigned char* rawMap, int size);

	int parse3ds (HANDLE file);

	void showNormals();

	void initRasterizerState(bool wireframe);

	sceneCamera* getCamera();

	void orbitMove(float x, float y);
	void orbitStart(float x, float y);

	void resetOrbit();
	void clear();

	Physixer* getPhysics();
	void inputAcc(string in);
	void setHold(bool b);

private:

	//initialization methods
	bool createSwapChainAndDevice( UINT width, UINT height );
	bool loadShadersAndCreateInputLayouts();
	void createViewports( UINT width, UINT height );
	
	bool createRenderTargetsAndDepthBuffer( UINT width, UINT height );

	//view orientation/control methods


	//fatal error handler
	bool fatalError(const LPCWSTR msg); 

	bool applyRagdoll();
};

#endif